﻿using dotNetMagazin201108.BusinessLayer;
using System.Collections.Generic;
using System.Linq;

namespace dotNetMagazin201108.PersistenceLayer
{
    public class ContactRepositoryImpl : ContactRepository
    {
        private List<Contact> DummyContacts;
        private int nextId = 1;
        private static ContactRepositoryImpl Instance = null;

        private ContactRepositoryImpl()
        {
            DummyContacts = new List<Contact>();
            DummyContacts.Add(CreateContact(nextId++, "Mueller", "Heinz", "", "", 2));
            DummyContacts.Add(CreateContact(nextId++, "Schmidt", "Franz", "", "", 3));
            DummyContacts.Add(CreateContact(nextId++, "Meier", "Hans-Peter", "hans-peter.meier@gmail.com", "+49 30 9886752", 1));
            DummyContacts.Add(CreateContact(nextId++, "Weber", "Sandra", "", "", 11));
            DummyContacts.Add(CreateContact(nextId++, "Scheider", "Annette", "", "", 2));
            DummyContacts.Add(CreateContact(nextId++, "Wagner", "Gustav", "", "", 2));
        }

        public static ContactRepositoryImpl GetInstance()
        {
            if (Instance == null) Instance = new ContactRepositoryImpl();
            return Instance;
        }

        public IEnumerable<Contact> GetAll()
        {
            List<Contact> entityList = new List<Contact>();
            entityList.AddRange(DummyContacts);
            return entityList;
        }
        public Contact GetById(int id)
        {
            Contact contact = DummyContacts.Where(c => c.Id == id).SingleOrDefault();
            return contact;
        }

        private Contact CreateContact(int id, string lastName, string firstName, string email, string phone, int companyId)
        {
            Contact contact = new Contact();

            contact.Id = id;
            contact.LastName = lastName;
            contact.FirstName = firstName;
            contact.Email = email;
            contact.Phone = phone;
            contact.CompanyId = companyId;

            return contact;
        }

        public bool Add(Contact contact)
        {
            return AddOrUpdate(contact);
        }

        public bool Update(Contact contact)
        {
            return AddOrUpdate(contact);
        }

        private bool AddOrUpdate(Contact contact)
        {
            if (contact.Id == 0)
            {
                contact.Id = nextId++;
                DummyContacts.Add(contact);
            }
            else
            {
                Contact originalContact = DummyContacts.Where(c => c.Id == contact.Id).Single();
                originalContact.FirstName = contact.FirstName;
                originalContact.LastName = contact.LastName;
                originalContact.Email = contact.Email;
                originalContact.Phone = contact.Phone;
                originalContact.CompanyId = contact.CompanyId;
            }

            return true;
        }
    }
}
